/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Ltd. 		   */
/*																   */
/*	NAME	  = t__sqlserver_bigint_add.c						   */
/*	NOTE	  = 												   */
/*	DATE	  = 2014/11/25 by geyh								   */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include "apmtest_cunit.h"
#include <db2/db2_core.h>
#include <db2/db2_froca.h>
#include <db2/config.h>

extern int db2_8byte_float(Frbyte_Packet *p_st_data, int i_whole_flg, int i_typdef, unsigned int *p_ui_len,
                           Frdata_field *p_st_frdata);

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

static void __case1(void)
{
    int i_ret = APM_L7_ANALYZE_SUCC;
    Frdata_field *p_st_frdata = NULL;
    Frbyte_Packet *p_st_data = NULL;
    unsigned int ui_len = 8;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x9a\x99\x99\x99\x99\x99\xf1\x3f", 8);
    p_st_frdata = (Frdata_field *) frmalloc(sizeof(Frdata_field));
    memset(p_st_frdata, 0x0, sizeof(Frdata_field));

    i_ret = db2_8byte_float(p_st_data, DB2_FALSE, DRDA_TYPDEF_QTDSQLX86, &ui_len, p_st_frdata);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_EQUAL(ui_len, 0);
    CU_ASSERT_STRING_EQUAL(p_st_frdata->p_field_value, "1.100000");
    frfree(p_st_frdata->p_field_value);
    frfree(p_st_frdata);
    bytebuff_free(&p_st_data);
    return;
}

static void __case2(void)
{
    int i_ret = APM_L7_ANALYZE_SUCC;
    Frdata_field *p_st_frdata = NULL;
    Frbyte_Packet *p_st_data = NULL;
    unsigned int ui_len = 8;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x85\xeb\x51\xb8\x1e\x05\x24\x40", 8);
    p_st_frdata = (Frdata_field *) frmalloc(sizeof(Frdata_field));
    memset(p_st_frdata, 0x0, sizeof(Frdata_field));

    i_ret = db2_8byte_float(p_st_data, DB2_FALSE, DRDA_TYPDEF_QTDSQLX86, &ui_len, p_st_frdata);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_EQUAL(ui_len, 0);
    CU_ASSERT_STRING_EQUAL(p_st_frdata->p_field_value, "10.010000");
    frfree(p_st_frdata->p_field_value);
    frfree(p_st_frdata);
    bytebuff_free(&p_st_data);
    return;
}

static void __case3(void)
{
    int i_ret = APM_L7_ANALYZE_SUCC;
    Frdata_field *p_st_frdata = NULL;
    Frbyte_Packet *p_st_data = NULL;
    unsigned int ui_len = 8;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\xf2\xd2\x4d\x62\x10\x00\x59\x40", 8);
    p_st_frdata = (Frdata_field *) frmalloc(sizeof(Frdata_field));
    memset(p_st_frdata, 0x0, sizeof(Frdata_field));

    i_ret = db2_8byte_float(p_st_data, DB2_FALSE, DRDA_TYPDEF_QTDSQLX86, &ui_len, p_st_frdata);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_EQUAL(ui_len, 0);
    CU_ASSERT_STRING_EQUAL(p_st_frdata->p_field_value, "100.001000");
    frfree(p_st_frdata->p_field_value);
    frfree(p_st_frdata);
    bytebuff_free(&p_st_data);
    return;
}

static void __case4(void)
{
    int i_ret = APM_L7_ANALYZE_SUCC;
    Frdata_field *p_st_frdata = NULL;
    Frbyte_Packet *p_st_data = NULL;
    unsigned int ui_len = 9;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x9a\x99\x99\x99\x99\x99\xf1\x3f", 8);
    p_st_frdata = (Frdata_field *) frmalloc(sizeof(Frdata_field));
    memset(p_st_frdata, 0x0, sizeof(Frdata_field));

    i_ret = db2_8byte_float(p_st_data, DB2_FALSE, DRDA_TYPDEF_QTDSQLX86, &ui_len, p_st_frdata);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_EQUAL(ui_len, 1);
    CU_ASSERT_STRING_EQUAL(p_st_frdata->p_field_value, "1.100000");
    frfree(p_st_frdata->p_field_value);
    frfree(p_st_frdata);
    bytebuff_free(&p_st_data);
    return;
}

static void __case5(void)
{
    int i_ret = APM_L7_ANALYZE_SUCC;
    Frdata_field *p_st_frdata = NULL;
    Frbyte_Packet *p_st_data = NULL;
    unsigned int ui_len = 7;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x9a\x99\x99\x99\x99\x99\xf1\x3f", 8);
    p_st_frdata = (Frdata_field *) frmalloc(sizeof(Frdata_field));
    memset(p_st_frdata, 0x0, sizeof(Frdata_field));

    i_ret = db2_8byte_float(p_st_data, DB2_FALSE, DRDA_TYPDEF_QTDSQLX86, &ui_len, p_st_frdata);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
    CU_ASSERT_EQUAL(ui_len, 7);
    frfree(p_st_frdata);
    bytebuff_free(&p_st_data);
    return;
}

static void __case6(void)
{
    int i_ret = APM_L7_ANALYZE_SUCC;
    Frdata_field *p_st_frdata = NULL;
    Frbyte_Packet *p_st_data = NULL;
    unsigned int ui_len = 8;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x9a\x99\x99\x99\x99\x99\xf1", 7);
    p_st_frdata = (Frdata_field *) frmalloc(sizeof(Frdata_field));
    memset(p_st_frdata, 0x0, sizeof(Frdata_field));

    i_ret = db2_8byte_float(p_st_data, DB2_FALSE, DRDA_TYPDEF_QTDSQLX86, &ui_len, p_st_frdata);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_TODO);
    CU_ASSERT_EQUAL(ui_len, 8);
    frfree(p_st_frdata);
    bytebuff_free(&p_st_data);
    return;
}

static void __case7(void)
{
    int i_ret = APM_L7_ANALYZE_SUCC;
    Frdata_field *p_st_frdata = NULL;
    Frbyte_Packet *p_st_data = NULL;
    unsigned int ui_len = 8;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x9a\x99\x99\x99\x99\x99\xf1", 7);
    p_st_frdata = (Frdata_field *) frmalloc(sizeof(Frdata_field));
    memset(p_st_frdata, 0x0, sizeof(Frdata_field));

    i_ret = db2_8byte_float(p_st_data, DB2_TRUE, DRDA_TYPDEF_QTDSQLX86, &ui_len, p_st_frdata);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
    CU_ASSERT_EQUAL(ui_len, 8);
    frfree(p_st_frdata);
    bytebuff_free(&p_st_data);
    return;
}

static void __case8(void)
{
    int i_ret = APM_L7_ANALYZE_SUCC;
    Frdata_field *p_st_frdata = NULL;
    Frbyte_Packet *p_st_data = NULL;
    unsigned int ui_len = 8;

    p_st_data = bytebuff_creat();
    p_st_frdata = (Frdata_field *) frmalloc(sizeof(Frdata_field));
    memset(p_st_frdata, 0x0, sizeof(Frdata_field));

    i_ret = db2_8byte_float(p_st_data, DB2_FALSE, DRDA_TYPDEF_QTDSQLX86, &ui_len, p_st_frdata);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_TODO);
    CU_ASSERT_EQUAL(ui_len, 8);
    frfree(p_st_frdata);
    bytebuff_free(&p_st_data);
    return;
}

int add_suite_db2_8byte_float(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" db2_8byte_float()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL ||
        CU_add_test(pSuite, "case4", __case4) == NULL ||
        CU_add_test(pSuite, "case5", __case5) == NULL ||
        CU_add_test(pSuite, "case6", __case6) == NULL ||
        CU_add_test(pSuite, "case7", __case7) == NULL ||
        CU_add_test(pSuite, "case8", __case8) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
